package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.Bean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.IUserService;
import com.crm.service.IListboxService;
import com.crm.model.User;
import com.crm.model.LProduct;
import com.crm.model.LCustLevel;
import com.crm.model.LCustSource;
import com.crm.web.util.Constants;


/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class Home extends BasePage implements PageBeginRenderListener {
    
    private static Log log = LogFactory.getLog(Home.class);

    @InjectSpring("userService")
    public abstract IUserService getUserService();

    @InjectState("userSession")
    public abstract User getUserSession();

    public abstract void setUserSession(User user);

    @InjectSpring("listboxService")
    public abstract IListboxService getListboxServ();

    @Bean
    public abstract ValidationDelegate getDelegate();

    public abstract String getUsername();

    public abstract String getPassword();


    // listener method of login form
    public String loginSubmit(IRequestCycle cycle) {
        String tempUsername = getUsername() == null ? "" : getUsername();
        String tempPassword = getPassword() == null ? "" : getPassword();

        if (getDelegate().getHasErrors()) {
            return getPageName();
        }

        User user = getUserService().getUserByNameAndPwd(tempUsername, tempPassword);
        if ((user != null)) {
            setUserSession(user);

            // set cookies value and use it if session was null, just show staff code to log and read it.
            // Don't use it hold on application because if you do so it will make a insecurity factor.
            this.getRequestCycle().getInfrastructure().getCookieSource().writeCookieValue("userid", getUserSession().getId());

            log.info(getUserSession().getUsername()
                    + " : User login success! \n"
                    + getUserSession().toString());
            return "Main";
        } else {
            log.warn(getUserSession().getUsername()
                    + " : Username or password is error! [username="
                    + tempUsername + ",password=" + tempPassword + "]");
            String message = "用户名或密码不正确！";
            getDelegate().record(message, ValidationConstraint.CONSISTENCY);
            return getPageName();
        }
    }

    public void pageBeginRender(PageEvent event) {     
         Constants.L_PRODUCT = getListboxServ().getList(LProduct.class.getName());
         Constants.L_CUST_LEVEL = getListboxServ().getList(LCustLevel.class.getName());
         Constants.L_CUST_SOURCE = getListboxServ().getList(LCustSource.class.getName());
        Constants.L_USER = getUserService().getUsers();
    }

}
